import sqlalchemy as sa
from sqlalchemy.orm import relationship
from ._base import BaseORM


class DepartmentORM(BaseORM):
    __tablename__ = "ums_department"
    id = sa.Column(sa.Integer, primary_key=True, comment="部门ID")
    name = sa.Column(sa.String(50), comment="部门名称")
    leader = sa.Column(sa.String(50), comment="负责人")
    enable = sa.Column(sa.Boolean, comment="状态(1开启,0关闭)", default=True)

    pid = sa.Column(sa.Integer, sa.ForeignKey("ums_department.id"), default=1)

    def json(self):
        return {
            "id": self.id,
            "pid": self.pid,
            "name": self.name,
            "leader": self.leader,
            "enable": self.enable,
        }

    users = relationship("UserORM", backref="department")

    parent = relationship(  # //[!code ++]
        "DepartmentORM", back_populates="children", remote_side=[id]  # //[!code ++]
    )  # 自关联 //[!code ++]
    children = relationship("DepartmentORM", back_populates="parent")  # //[!code ++]
